package com.alameda.service.impl.danmaku;

import com.alameda.common.commonpent.commonback.api.CommonBackTemplate;
import com.alameda.common.utils.ObjectUtils;
import com.alameda.danmaku.dto.ApVideoDanmakuDTO;
import com.alameda.danmaku.vo.ApVideoDanmakuVO;
import com.alameda.facade.video.ApVideoFacade;
import com.alameda.mapper.danmaku.ApVideoDanmakuMapper;
import com.alameda.pojo.danmaku.ApVideoDanmakuDO;
import com.alameda.service.danmaku.ApVideoDanmakuService;
import com.alameda.statement.enums.AdSystemRecordEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.alameda.common.base.constant.SqlDmlConstant.DELETE_AP_VIDEO_DANMAKU;

/**
 * 视频弹幕 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2024/10/07
 */
@Slf4j
@Service
public class ApVideoDanmakuServiceImpl extends ServiceImpl<ApVideoDanmakuMapper, ApVideoDanmakuDO> implements ApVideoDanmakuService {

    @Autowired
    private ApVideoDanmakuMapper apVideoDanmakuMapper;

    /**
     * 查询弹幕
     *
     * @param apVideoDanmakuDTO 视频弹幕信息
     * @return
     */
    @Override
    public List<ApVideoDanmakuVO> load(ApVideoDanmakuDTO apVideoDanmakuDTO) {
        List<ApVideoDanmakuDO> apVideoDanmakuDOList = this.lambdaQuery()
                .in(ApVideoDanmakuDO::getVideoId, apVideoDanmakuDTO.getVideoId())
                .orderByAsc(ApVideoDanmakuDO::getCreateTime)
                .list();

        return ObjectUtils.convertList(apVideoDanmakuDOList, ApVideoDanmakuVO.class);
    }

    @Autowired
    private ApVideoFacade apVideoFacade;

    /**
     * 保存弹幕
     *
     * @param apVideoDanmakuDO 视频弹幕
     * @return
     */
    @Override
    public Boolean saveDanmaku(ApVideoDanmakuDO apVideoDanmakuDO) {
        apVideoFacade.saveRecord(AdSystemRecordEnum.DATA_COUNT);
        return this.save(apVideoDanmakuDO);
    }

    /**
     * 删除视频弹幕
     *
     * @param idList 媒体文件id集合
     * @return
     */
    @Override
    public Boolean removeDanmakuByMediaIdList(List<Long> idList) {
        LambdaUpdateWrapper<ApVideoDanmakuDO> lambdaUpdateWrapper = Wrappers.lambdaUpdate(ApVideoDanmakuDO.class).in(ApVideoDanmakuDO::getVideoId, idList);
        return this.remove(lambdaUpdateWrapper);
    }

    @SuppressWarnings("all")
    @Autowired
    private CommonBackTemplate commonBackTemplate;

    /**
     * 物理层面删除已逻辑删除超过七天的视频弹幕
     */
    @Override
    public Boolean removeLogicDeletedFileDanmaku() {
        commonBackTemplate.executeDelete(DELETE_AP_VIDEO_DANMAKU);
        return Boolean.TRUE;
    }
}
